/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package android.web.service;

import android.app.ContextoAndroid;
import android.app.Mensaje;
import android.content.Context;
import android.net.ConnectivityManager;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author payo
 */
public class WebService {
    
    private static String getDatos(ArrayList<NameValuePair> nameValuePairs,String php)
    {
        String result = "";
        InputStream is = null;
        StringBuilder sb=null;
        try{
            HttpClient httpclient = new DefaultHttpClient();
            String ruta = "http://"+IPServer.Server+"/"+php;
            HttpPost httppost = new HttpPost(ruta);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line="0";
            while ((line = reader.readLine()) != null)
                sb.append(line + "\n");
            is.close();
            result=sb.toString();
            return result;
        }
        catch(Exception e){
            Mensaje.Mostrar(e.toString());
            return ("Error de Conexion DUDE!"+e.toString());
        }
    }
    
    public static JSONArray getJSONArray(ArrayList<NameValuePair> nvp , String JSONArray , String php)
    {
        try{
            String result = getDatos(nvp,php);
            JSONArray jArray =null;
            JSONObject json = new JSONObject(result);
            jArray = json.getJSONArray(JSONArray);
            return jArray;
        }
        catch(Exception e)
        {
            Mensaje.Mostrar(e.toString());
            return null;
        }
    }
    
    public static void HttpPost(ArrayList<NameValuePair> nameValuePairs , String php)
    {
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://"+IPServer.Server+"/" + php);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);
        }
        catch(Exception e){
            Mensaje.Mostrar(e.toString());
        }
    }
    
    public static boolean conexionInternert()
    {
        ConnectivityManager manager = (ConnectivityManager)ContextoAndroid.context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
        boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
        return ((isWifi || is3g));
    }
}
